from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail, Message
from config import Config

db = SQLAlchemy()
mail = Mail()
def create_app():
    app = Flask(__name__)
    app.config.from_object(Config)

    db.init_app(app)
    mail.init_app(app)

    # 注册蓝图或路由（如果有）
    #from app.routes import bp
    #app.register_blueprint(bp)

    return app

app = create_app()

@app.route('/test-email', methods=["GET"])
def test_email():
    try:
        msg = Message(
            subject="测试邮件主题",
            sender=app.config['MAIL_DEFAULT_SENDER'],
            recipients=['will_fightingboy@163.com'],  # 替换为收件人邮箱
        )
        # 邮件正文
        msg.body = f"""
        姓名: ll
        邮箱: 163
        留言: 111
        """

        # 添加附件（关键步骤）
        msg.attach(
            filename=file.filename,  # 保留原始文件名
            content_type=file.content_type,  # 自动检测 MIME 类型
            data=file.read()  # 读取文件内容
        )
        mail.send(msg)
        return "邮件发送成功！"
    except Exception as e:
        return f"邮件发送失败：{str(e)}"

if __name__ == '__main__':
    app.run(debug=True)